﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BilbilDAL;
using Common;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Modles.DTO;
using Modles.Models;

namespace BilbilBLL
{
    public class HomeServer
    {
        //初始DAL层
        CarouselDAL carouselDAL = new CarouselDAL();
        VideoInfoDAL videoInfoDAL = new VideoInfoDAL();
        VideoTypeDAL videoType = new VideoTypeDAL();
        SearchForRecordDAL searchForRecord = new SearchForRecordDAL();
        VideoHistoryDAL videoHistoryDAL  =new VideoHistoryDAL();

        /// <summary>
        /// 打印轮播图表按id降序排序
        /// </summary>
        /// <returns></returns>
        public Response_DTO<List<Carousel_DTO>> GetCarousel()
        {
            return new Response_DTO<List<Carousel_DTO>>()
            {
                Conten = carouselDAL.GetCarousel(),
                Success = true,
                Message = "获取轮播图成功"
            };
        }
        /// <summary>
        /// 打印轮播图表2按id降序排序
        /// </summary>
        /// <returns></returns>
        public ResponseTool GetCarouselTwo()
        {
            return ResponseTool.Succeed(new CarouselTwoDAL().GetCarouselTwo());
        }

        /// <summary>
        /// 获取主页视频信息（时间降序排序，提供分页功能）
        /// </summary>
        /// <param name="model"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public ResponseTool GetVideoInfo(int page, int limit, out int TotalCount)
        {
            TotalCount = 0;
            //数据校验
            if (page <= 0)
            {
                return ResponseTool.Fail("页码不得小于1");
            }



            List<VideoInfo_DTO> list = videoInfoDAL.GetVideoInfo(page, limit, out TotalCount);
            ResponseTool res = ResponseTool.Succeed(list);
            return res;
        }
        /// <summary>
        /// 打印视频信息表，按播量排序，仅输出前100个视频信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public ResponseTool GetVideoInfoSort()
        {
            List<VideoInfo_DTO> list = videoInfoDAL.GetVideoInfoSort();
            ResponseTool res = ResponseTool.Succeed(list);
            return res;
        }
        /// <summary>
        /// 根据视频信息表的视频标题模糊查询，打印查询到的视频信息表按视频发布时间降序排序，提供分页功能
        /// </summary>
        /// <param name="model"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public ResponseTool GetVideoInfoBytitle(StudentQuery_DTO model, out int TotalCount)
        {
            TotalCount = 0;
            if (model.page <= 0)
            {
                return ResponseTool.Fail("页码不能小于1");
            }

            List<VideoInfo_DTO> list = videoInfoDAL.GetVideoInfoBytitle(model, out TotalCount);
            return ResponseTool.Succeed(list);
        }
        /// <summary>
        /// 获取视频类型
        /// </summary>
        /// <returns></returns>
        public Response_DTO<List<VideoType_DTO>> GetVideoType()
        {
            List<VideoType_DTO> video = videoType.GetVideoType();
            Response_DTO<List<VideoType_DTO>> response = new Response_DTO<List<VideoType_DTO>>()
            {
                Conten = video,
                Message = "成功",
                StatuCode = 200,
                Success = true
            };
            return response;
        }

        /// <summary>
        /// 搜索记录持久化
        /// </summary>
        /// 
        public void AddSearchForRecord(SearchForRecord_DTO dto)
        {
            searchForRecord.AddSearchForRecord(dto);
        }

        /// <summary>
        /// 获取热门搜索
        /// </summary>
        /// <returns></returns>
        public Response_DTO<List<SearchForRecord>> GetSearchForRecords()
        {
            List<SearchForRecord> list = searchForRecord.GetSearchForRecords();
            Response_DTO<List<SearchForRecord>> response = new Response_DTO<List<SearchForRecord>>()
            {
                Conten = list,
                Message = "OK",
                Success = true,
                StatuCode = 200
            };
            return response;
        }


        /// <summary>
        /// 获取观看已视频的历史记录
        /// </summary>
        /// <param name="UserInfoId"></param>
        /// <returns></returns>
        public ResponseTool GetVideoHistory(int UserInfoId)
        {
            if (UserInfoId == 0)
            {

                return ResponseTool.Fail("用户信息id不能为空");
            }
            List<VideoInfo_DTO> list = videoHistoryDAL.GetVideoHistory(UserInfoId);
            ResponseTool.Succeed(list);
            return ResponseTool.Succeed(list);
        }
    }
}
